//
//  AppDelegate.m
//  HaiTongHop
//
//  Created by Nguyen Cong Huy on 12/16/14.
//  Copyright (c) 2014 Nguyen Cong Huy. All rights reserved.
//

#import "AppDelegate.h"
#import "MainViewController.h"
#import "GAIDictionaryBuilder.h"
#import "Utils.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc] init]];
    [self.window makeKeyAndVisible];
    
    if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound categories:nil]];
    }
    
    [self initializeGoogleAnalytic];
    
    return YES;
}

- (void)initializeGoogleAnalytic {
    [GAI sharedInstance].trackUncaughtExceptions = YES;
    [[GAI sharedInstance] trackerWithTrackingId:@"UA-42372156-9"];
    
    [Utils sendTrackWithCategory:@"Application" action:@"Start_app" label:@"Start_app" value:nil];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self addLocalNotification];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self cancelAllLocalNotification];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [self addLocalNotification];
}

- (void)addLocalNotification {
    [self cancelAllLocalNotification];
    
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:86400];
    localNotification.alertBody = @"Đã lâu rồi bạn chưa vào Hài Tổng Hợp. Có rất nhiều ảnh, video mới. Mời bạn nhấn để xem ảnh mới";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

- (void)cancelAllLocalNotification {
    NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
    for(UILocalNotification *notification in notificationArray){
        [[UIApplication sharedApplication] cancelLocalNotification:notification] ;
    }
}

@end
